package com.yoso.shop.interfaces.controller.cart;

import com.alibaba.fastjson.JSON;
import com.yoso.base.interfaces.controller.BaseController;
import com.yoso.base.interfaces.controller.BaseResponse;
import com.yoso.base.service.IBaseService;
import com.yoso.shop.application.service.ICartService;
import com.yoso.shop.domain.cart.CartEntity;
import com.yoso.shop.domain.cart.CartEntityExample;
import com.yoso.shop.interfaces.vo.cart.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yoso
 * @date 2022/1/31
 */
@RestController
@RequestMapping("/cart")
@Slf4j
@CrossOrigin
@Api(tags = "购物车")
public class CartController extends BaseController<CartVo, CartEntity> {
    @Autowired
    private ICartService cartService;

    @Override
    public CartEntity V2E(CartVo data) {
        return JSON.parseObject(JSON.toJSONString(data), CartEntity.class);
    }

    @Override
    public CartVo E2V(CartEntity data) {
        return JSON.parseObject(JSON.toJSONString(data), CartVo.class);

    }

    @Override
    public IBaseService<CartVo, CartEntityExample> getService() {
        return this.cartService;
    }

    @PostMapping("getCartByUsername")
    @ApiOperation("根据用户名获取购物车列表")
    public BaseResponse getCartByUsername(@RequestParam("username") String username) {
        return this.success(this.cartService.getCartByUsername(username));
    }
}
